using System;
using System.Linq;
using NHibernate;
using Wikey.Domain.Components;
using Wikey.Entity;

namespace Wikey.Domain.Repository
{
    public interface IContentNodeRepository : IQueryableRepository<ContentNode>
    {
        int Save(ContentNode contentNode);
        ContentNode Find(int id);
        bool Delete(int id);
    }

    public class ContentNodeRepository : Repository ,  IContentNodeRepository
    {
        public ContentNodeRepository(ISessionSource source) : base(source)
        {
        }

        public ContentNodeRepository(ISession session) : base(session)
        {
        }

        public int Save(ContentNode contentNode)
        {
            base.Save(contentNode);

            return contentNode.Id;
        }

        public ContentNode Find(int id)
        {
            return Context().Get<ContentNode>().Where(p => p.Id == id).FirstOrDefault();
        }

        public bool Delete(int id)
        {
            base.Delete(Find(id));
            return true;
        }

        public IQueryable<ContentNode> Selectable()
        {
            throw new NotImplementedException();
        }
    }
}